﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Suppliers
{
    public partial class New : System.Web.UI.Page
    {
        #region Properties

        private SupplierBiz _supplierBiz;
        private SupplierBiz SupplierManager
        {
            get { return _supplierBiz ?? (_supplierBiz = new SupplierBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // load supplier groups
                var groups = SupplierManager.GetAllSupplierGroups();//_repository.CityRepository.GetAllSupplierGroups();
                groups.Insert(0, new SupplierGroup { Id = 0 });

                ddlSupplierGroups.DataTextField = "Name";
                ddlSupplierGroups.DataValueField = "Id";
                ddlSupplierGroups.DataSource = groups;
                ddlSupplierGroups.DataBind();

                // get auto code
                var nextCode = SupplierManager.GetNextCode();
                txtCode.Text = string.Format("{0:00000}", nextCode);
                txtSortOrder.Text = nextCode.ToString(CultureInfo.InvariantCulture);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var address = txtAddress.Text.Trim();
            var phone = txtPhone.Text.Trim();
            var fax = txtFax.Text.Trim();
            var email = txtEmail.Text.Trim();
            var website = txtWebsite.Text.Trim();
            var taxcode = txtTaxCode.Text.Trim();
            var bankAccount = txtBankAccount.Text.Trim();
            var bankName = txtBankName.Text.Trim();
            var bankProvince = txtBankProvince.Text.Trim();
            var director = txtDirectorName.Text.Trim();
            var accounter = txtAccounterName.Text.Trim();
            int supplierGroupId;
            int.TryParse(ddlSupplierGroups.SelectedValue, out supplierGroupId);

            // get data
            var newSupplier = new Supplier
            {
                Code = code,
                Name = name,
                Address = address,
                Phone = phone,
                Fax = fax,
                Email = email,
                Website = website,
                TaxCode = taxcode,
                BankAccount = bankAccount,
                BankName = bankName,
                BankProvince = bankProvince,
                DirectorName = director,
                AccounterName = accounter,
                Description = descrition,
                SortOrder = sortOrder,
                Hidden = isHidden
            };

            if (supplierGroupId > 0) newSupplier.SupplierGroupId = supplierGroupId;

            // save data
            SupplierManager.CreateSupplier(newSupplier);

            // return
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}